<?php
include_once('common/header.php');
if($AppUI->id <= 0 || (isset($_SESSION['event']) && (($_SESSION['event']&1<<1) == 0))) {
	$this->_redirect(HOST_BACKEND);
}
//params
$intCategoryId		= (int)$objRequest->getParam('pcategoryid',0);
$strName			= $objRequest->getParam('pname','');
$dblLng				= (double)$objRequest->getParam('plongitude',0.0);
$dblLat				= (double)$objRequest->getParam('platitude',0.0);
$strCountry			= $objRequest->getParam('pcountry',COUNTRY_DEFAULT);
$intProvince		= (int)$objRequest->getParam('pprovince',0);
$intDistrict		= (int)$objRequest->getParam('pdistrict',0);
$strAddr			= $objRequest->getParam('paddr','');
$strFullAddr		= $objRequest->getParam('pfulladdr','');
$strStart			= trim($objRequest->getParam('pdstart',''));
$strStartH			= trim($objRequest->getParam('pdstart_hour',''));
$strStartM			= trim($objRequest->getParam('pdstart_minute',''));
$strEnd				= trim($objRequest->getParam('pdend',''));
$strEndH			= trim($objRequest->getParam('pdend_hour',''));
$strEndM			= trim($objRequest->getParam('pdend_minute',''));
$intPrice			= (int)$objRequest->getParam('pprice',0);
$intAfrom			= (int)$objRequest->getParam('pafrom',0);
$intAto				= (int)$objRequest->getParam('pato',0);
$intQty				= (int)$objRequest->getParam('pqty',0);
$intQtyReg			= (int)$objRequest->getParam('pqtyreg',0);
$strShort			= $objRequest->getParam('pshort','');
$strContent			= $objRequest->getParam('pcontent','');
$strContact			= $objRequest->getParam('pcontact','');
$strTag				= $objRequest->getParam('ptag','');
$intAdv				= $objRequest->getParam('padv',0);
$oFile				= $_FILES['pimage'];
$intAlbumId			= $objRequest->getParam('palbumid',0);
$strFeature			= $objRequest->getParam('ptbfeature','');
$arrErr				= null;
$strImage			= '';
//data
$strName			= Ecore_Function::stripTags($strName,-2);
$strAddr			= Ecore_Function::stripTags($strAddr,-2);
$strContent			= Ecore_Function::stripTags($strContent,-1);
$strContact			= Ecore_Function::stripTags($strContact,-1);
$strShort			= Ecore_Function::stripTags($strShort,-2);
$strTag				= Ecore_Function::stripTags($strTag,-2);
$strFeature			= Ecore_Function::stripTags($strFeature,0);
//validate
if($intCategoryId < 1) array_push($arrErr, $arrLocale['category_required']);
if($strName == '') array_push($arrErr, $arrLocale['name_required']);
elseif($strName != '' && strlen($strName) < VL_NAME_MINLENGTH) array_push($arrErr, $arrLocale['name_minlength']);
if($intProvince < 1) array_push($arrErr, $arrLocale['province_required']);
if($intDistrict < 1) array_push($arrErr, $arrLocale['district_required']);
if($strAddr == '') array_push($arrErr, $arrLocale['addr_required']);
if($strStart == '' || $strStartH == '' || $strStartM == '') array_push($arrErr, $arrLocale['start_required']);
elseif((int)$strStartH > 23 || (int)$strStartM > 60) array_push($arrErr, $arrLocale['start_format']);
if($strEnd == '' || $strEndH == '' || $strEndM == '') array_push($arrErr, $arrLocale['end_required']);
elseif((int)$strEndH > 23 || (int)$strEndM > 60) array_push($arrErr, $arrLocale['end_format']);
if($strShort != '' && strlen($strShort) < VL_SHORT_MINLENGTH) array_push($arrErr, $arrLocale['short_minlength']);
elseif($strShort != '' && strlen($strShort) > VL_SHORT_MAXLENGTH) array_push($arrErr, $arrLocale['short_maxlength']);
if($strContent == '') array_push($arrErr, $arrLocale['content_required']);
elseif($strContent != '' && strlen($strContent) < VL_CONTENT_MINLENGTH) array_push($arrErr, $arrLocale['content_minlength']);

if(!isset($arrErr) && isset($oFile)) {
	$arrImage	= Ecore_Upload_Event::upload($oFile);
	if($arrImage['error'] == 0) {
		$strImage	= $arrImage['path'];
	}else {
		array_push($arrErr, $arrLocale['upload']);
	}
}
//insert
if(!isset($arrErr)) {
	if(isset($_SESSION['EVENT_POST'])) unset($_SESSION['EVENT_POST']);
	if(isset($_SESSION['EVENT_ERROR'])) unset($_SESSION['EVENT_ERROR']);
	$strStartH			= ((int)$strStartH < 10) ? ('0'.$strStartH) : $strStartH;
	$strStartM			= ((int)$strStartM < 10) ? ('0'.$strStartM) : $strStartM;
	$strEndH			= ((int)$strEndH < 10) ? ('0'.$strEndH) : $strEndH;
	$strEndM			= ((int)$strEndM < 10) ? ('0'.$strEndM) : $strEndM;
	$strDsart			= Ecore_Function::convertStrToDate($strStart) . ' ' . $strStartH . ':' . $strStartM . ':00';
	$strDend			= Ecore_Function::convertStrToDate($strEnd) . ' ' . $strEndH . ':' . $strEndM . ':00';
	$intUserId			= (-1)*$AppUI->id;	
	$strShort			= $strShort == '' ? Ecore_Function::wordCut($strContent) : $strShort; 
	if(isset($strFullAddr) && ($dblLng == '' || $dblLng == 0.0 || $dblLat == '' || $dblLat == 0.0)) {
		$arrLocation 	= Ecore_Function::getCoordinatesFromAddress($strFullAddr, $strCountry);		
		$dblLng			= $arrLocation->lng;
		$dblLat			= $arrLocation->lat;		
	} else {
		$dblLng			= str_replace(',','.',$dblLng);
		$dblLat			= str_replace(',','.',$dblLat);	
	}
	$intEventId			= 0;
	$intSubId			= 0;
	$objEvent 			= new Event();
	$intError			= $objEvent->add($AppUI->id, $intUserId, $strName, $intCategoryId, $intSubId,
										 $strAddr, $intDistrict, $intProvince, $strCountry, $dblLng,
										 $dblLat, $strDsart, $strDend, $intAfrom, $intAto,
										 $intQty, $intQtyReg, $strImage, $strShort, $strContent,
										 $strTag, $intAdv, $strContact, $intPrice, $intAlbumId,
										 $strFeature, EVENT_TYPE);
	if($intError == 0) Ecore_Function::setMsg($arrMessage['event_add']);
	else array_push($arrErr, $arrLocale['noperm_event_add']);
}
if($intError > 0) {
	$_SESSION['EVENT_POST']	= $objRequest->getParams();
	$_SESSION['EVENT_ERROR']= $arrErr;
	$this->_redirect(HOST_BACKEND . 'event/add');
}
$this->_redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND . 'event/index');
